/**
 * 
 */
package com.gmail.blackdog1987.carcare.network.response.handler;

import java.util.ArrayList;

import org.json.JSONObject;

import com.gmail.blackdog1987.carcare.entity.TCarchangeshop;
import com.gmail.blackdog1987.carcare.entity.base.DataPage;
import com.gmail.blackdog1987.carcare.network.ResponseCustomError;
import com.gmail.blackdog1987.carcare.network.response.CarChangeShopResponse;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.util.LogUtil;
import com.google.gson.Gson;

/**
 * @author wdy
 *
 */
public class CarChangeShopResponseHandler extends BaseResponseHandler {

	public static final String TAG = CarChangeShopResponseHandler.class.toString();
	/** 监听类 */
	private OnResponseListener mListener;
	
	private CarChangeShopResponse mResponse;
	
	public void setOnResponseListener(OnResponseListener l) {
		this.mListener = l;
	}

	@Override
	public void onFailure(String responseBody, Throwable error) {
		mResponse = new CarChangeShopResponse();
		if(!checkConn(error)) {
			mResponse.error = IResponseHandler.CONNECTION_ERROR;
		} else {
			if(responseBody != null) {
				LogUtil.e(TAG, responseBody);
				mResponse.error = new ResponseCustomError(responseBody);
			} else {
				LogUtil.e(TAG, responseBody);
				mResponse.error = IResponseHandler.CONNECTION_ERROR;
			}
		}
		
		if(mListener != null) {
			mListener.onError(this);
		}
	}
	
	@Override
	public void onSuccess(JSONObject response) {
		boolean result = this.parseJson(response);
		if(mListener == null) {
			return;
		}
		
		if(!result) {
			mListener.onError(this);
		} else {
			mListener.onResponse(this);
		}
	}
	
	public boolean parseJson(Object response) {
		mResponse = new CarChangeShopResponse();
		try {
			if(response != null) {
				LogUtil.d(CarChangeShopResponseHandler.class.toString(), response.toString());
				// 错误信息处理
				JSONObject json = (JSONObject)response;
				if(!(Boolean) json.get("success")) {
					mResponse.error = new ResponseCustomError(json.toString());
					return false;
				}
				
				JSONObject record = json.getJSONObject("record");
				
				mResponse.datas = new DataPage<TCarchangeshop>();
				mResponse.datas.currentPageNo = record.getInt("currentPageNo");
				mResponse.datas.firstPage = record.getBoolean("firstPage");
				mResponse.datas.lastPage = record.getBoolean("lastPage");
				mResponse.datas.lastPageNo = record.getInt("lastPageNo");
				mResponse.datas.nextPage = record.getInt("nextPage");
				mResponse.datas.pageSize = record.getInt("pageSize");
				mResponse.datas.prePage = record.getInt("prePage");
				mResponse.datas.totalCount = record.getInt("totalCount");
				mResponse.datas.datas = new ArrayList<TCarchangeshop>();
				org.json.JSONArray jsonArray = record.getJSONArray("data");
				int len = jsonArray.length();
				for (int i = 0; i < len; i++) {
					JSONObject jsonObj = jsonArray.getJSONObject(i);
					TCarchangeshop obj= new TCarchangeshop();
					obj = new Gson().fromJson(jsonObj.toString(), TCarchangeshop.class); 
					mResponse.datas.datas.add(obj);
				}
			}
		} catch (Exception e) {
			LogUtil.e(CarChangeShopResponseHandler.class.toString(), e.getMessage());
			mResponse.error = IResponseHandler.JSON_PARSE_ERROR;
			if(checkWLAN(response)) {
				mResponse.error = IResponseHandler.CONNECTION_ERROR;
			}
		}
		return true;
	}

	@Override
	public IResponse getResponse() {
		return this.mResponse;
	}
}
